﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.IO;
using System.Text;
using System.Data.OracleClient;

namespace BSC_SERVICES.Code
{
    public class CL_Ultility
    {
        public static string messeseboxThemmoiThanhCong = "Thêm mới thành công";
        public static string messeseboxSuaThanhCong = "Sửa thành công";
        public static string messeseboxSuaThanhCong_CanDuyet = "Sửa bản ghi thành công, cần kiểm soát duyệt.";
        public static string messeseboxDuyetThanhCong = "Duyệt thành công.";
        public static string messeseboxMaSuKienKhongTonTai = "Mã sự kiện không tồn tại.";
        public static string ServicePath = HttpContext.Current.Server.MapPath("~/");

        BSC_SERVICES.Code.Common _common = new Code.Common();
        StreamWriter VietFileText;

        public void CREATE_LOG_FILE(string Log_type)
        {
            try
            {
                //Log_type LOG
                //Log_type ERR
                string File_name = _common.Current_DATE("yyyymmdd");
                string v_AppPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
                string log_folder = v_AppPath + "Logs";
                string file_path = "";
                if (Log_type == "LOG")
                {
                    file_path = log_folder + "\\" + File_name + "_LOG.log";
                }
                else if (Log_type == "ERR")
                {
                    file_path = log_folder + "\\" + File_name + "_ERR.log";
                }
                if (File.Exists(file_path) == false)
                {
                    if (Directory.Exists(log_folder) == false)
                    {
                        Directory.CreateDirectory(log_folder);
                    }
                    VietFileText = File.CreateText(file_path);
                    VietFileText.Close();
                }
            }
            catch (Exception ex)
            {

            }

        }

        public void UPDATE_LOG_FILE(string pv_logtype, string PV_TLID, string p_Sukien, string p_hamgoi, string Err_str)
        {
            try
            {

                //Log_type LOG
                //Log_type ERR
                string File_name = _common.Current_DATE("yyyymmdd");
                string v_AppPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
                string log_folder = v_AppPath + "Logs";
                string file_path = "";
                if (pv_logtype == "LOG")
                {
                    file_path = log_folder + "\\" + File_name + "_LOG.log";
                }
                else if (pv_logtype == "ERR")
                {
                    file_path = log_folder + "\\" + File_name + "_ERR.log";
                }

                if (File.Exists(file_path) == false)
                {
                    CREATE_LOG_FILE(pv_logtype);
                }
                //--------------------------------------
                VietFileText = File.AppendText(file_path);
                VietFileText.WriteLine("====" + PV_TLID + "===" + _common.Current_DATE("dd/mm/yyyy hh:pp:ss") + "========================================================");
                VietFileText.WriteLine("  Sự kiện : " + p_Sukien);
                VietFileText.WriteLine("  Hàm gọi : " + p_hamgoi);
                VietFileText.WriteLine("  Logtype : " + pv_logtype);
                VietFileText.WriteLine("  ex.tostring : " + Err_str);
                VietFileText.WriteLine("-------------------------------------------------------------------------------------------------");
                VietFileText.WriteLine("  ");
                VietFileText.Close();

            }
            catch (Exception ex)
            {

            }
        }

        public OracleType RowType(string _e)
        {
            switch (_e)
            {
                case "T":
                    return OracleType.VarChar;
                case "N":
                    return OracleType.Number;
                case "B":
                    return OracleType.VarChar;
                case "D":
                    return OracleType.DateTime;
                default:
                    return OracleType.VarChar;
            }
        }

        public byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            using (var ms = new MemoryStream())
            {
                imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                return ms.ToArray();
            }
        }

        // ham nay convert tu file --> byte[]
        public byte[] GetDocument(string _documentName)
        {
            string strdocPath;
            strdocPath = ServicePath + @"\Reports\" + _documentName;

            FileStream objfilestream = new FileStream(strdocPath, FileMode.Open, FileAccess.Read);
            int len = (int)objfilestream.Length;
            Byte[] documentcontents = new Byte[len];
            objfilestream.Read(documentcontents, 0, len);
            objfilestream.Close();

            return documentcontents;
        }

        // ham nay convert tu file --> byte[]
        public bool SaveDocument(string docname, Byte[] docbinaryarray)
        {
            FileStream objfilestream = new FileStream(ServicePath + @"\Reports\" + docname, FileMode.Create, FileAccess.ReadWrite);
            objfilestream.Write(docbinaryarray, 0, docbinaryarray.Length);
            objfilestream.Close();

            return true;
        }

        public string DataTableToString(DataTable dt)
        {
            string _str = "";
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Columns.Count; i++)
                    _str += dt.Rows[0][i].ToString() + ".";

                _str = _str.Substring(0, _str.Length - 1);
            }

            return _str;
        }
    }
}